import React from 'react'
import PropTypes from 'prop-types'
import { routerRedux } from 'dva/router'
import { connect } from 'dva'
import { Row, Col, Button, Popconfirm } from 'antd'
import { List, Page } from 'components'
import queryString from 'query-string'
import Filter from './Filter'
import Modal from './Modal'
import { ListMenuType } from 'utils/enums'

const ServiceFee = ({ location, dispatch, servicefee, loading }) => {
  location.query = queryString.parse(location.search)
  const { list, pagination, currentItem, modalVisible, modalType, selectedRowKeys } = servicefee
  const { pageSize } = pagination
  //弹出框属性, ceate可创建一个弹出框, 如果细节多就是跳转路由然后进入detail/new, 少就是一个弹出框
  const modalProps = {
    item: modalType === 'create' ? {} : currentItem,
    visible: modalVisible,
    maskClosable: false,
    confirmLoading: loading.effects['servicefee/update'],
    title: `${modalType === 'create' ? 'Create' : 'Update'}`,//弹出框标题
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      dispatch({
        type: `servicefee/${modalType}`,
        payload: data,
      })
    },
    onCancel () {
      dispatch({
        type: 'servicefee/hideModal',
      })
    },
  }
  //列表属性
  const listProps = {
    dataSource: list,
    menuType: ListMenuType.MENU_UPDATE + ListMenuType.MENU_DELETE,
    loading: loading.effects['servicefee/list'], //进入路由就要请求一个list
    pagination,
    location,
    columnsData: [
      {
        title: 'Service Fee',
        dataIndex: 'serviceFee',
        key: 'serviceFee',
      },
      {
        title: 'Memo',
        dataIndex: 'memo',
        key: 'memo',
      },
    ],
    onChange (page) {
      const { query, pathname } = location
      dispatch(routerRedux.push({
        pathname,
        search: queryString.stringify({
          ...query,
          page: page.current,
          pageSize: page.pageSize,
        }),
      }))
    },
    onAdd () {
      dispatch({
        type: 'servicefee/updateModalState',   //将modal打开, 并将新的数据(为空)注入modal
        payload: {
          modalType: 'create',
        },
      })
    },
    //删除某条
    onDeleteItem (id) {
      dispatch({
        type: 'servicefee/delete',
        payload: id,
      })
    },
    //多行选中
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'servicefee/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
    onEditItem (item) {   //打开modal时会调用这个函数, 这个在list中定义
      dispatch({
        type: 'servicefee/showModal',
        payload: {
          modalType: 'update',
          currentItem: item,
        },
      })
    },
  }

  const filterProps = {
    filter: {
      ...location.query,
    },
    onFilterChange (value) {
      dispatch(routerRedux.push({
        pathname: location.pathname,
        search: queryString.stringify({
          ...value,
          page: 1,
          pageSize,
        }),
      }))
    },
    onSearch (fieldsValue) {
      fieldsValue.keyword.length ? dispatch(routerRedux.push({
        pathname: '/servicefee',
        query: {
          field: fieldsValue.field,
          keyword: fieldsValue.keyword,
        },
      })) : dispatch(routerRedux.push({
        pathname: '/servicefee',
      }))
    },
  }
  //多选删除
  const handleDeleteItems = () => {
    dispatch({
      type: 'servicefee/multiDelete',
      payload: {
        ids: selectedRowKeys,
      },
    })
  }
  const onAdd = () => {
    dispatch({
      type: 'servicefee/updateModalState',   //将modal打开, 并将新的数据(为空)注入modal
      payload: {
        modalType: 'create',
      },
    })
  }
  return (
    <Page inner>
      <Row style={{ marginBottom: 10 }} type="flex" justify="end">
        <Col>
          <Button size="large" type="ghost" onClick={onAdd}>Create</Button>
        </Col>
      </Row>
      {
        selectedRowKeys.length > 0 &&
          <Row style={{ marginBottom: 12, fontSize: 13 }} type="flex" justify="end">
            <Col>
              {`Selected ${selectedRowKeys.length} items `}
              <Popconfirm title={'Are you sure delete these items?'} placement="left" onConfirm={handleDeleteItems}>
                <Button type="primary" size="large" style={{ marginLeft: 8 }}>Remove</Button>
              </Popconfirm>
            </Col>
          </Row>
      }
      <List {...listProps} />
      {modalVisible && <Modal {...modalProps} /> }
    </Page>
  )
}

ServiceFee.propTypes = {
  servicefee: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({ servicefee, loading }) => ({ servicefee, loading }))(ServiceFee)
